var INTERVAL = 1200;
var DEFULT_DURATION = 800;

/**
 * 开场渐入动画
 * @param {object}   that   调用对象
 * @param {str}     title   页面标题
 * @param {str}     theme   主题颜色
 * @param {int}  duration   动画时长
 */
function onShow(that, title, theme, duration, cb) {
  duration = duration || DEFULT_DURATION,
  theme = theme || '#ffffff';
  // 如果是 custom 导航栏请给 title 赋 'custom' 值
  if (title != 'custom') wx.setNavigationBarColor({
    frontColor: theme == '#ffffff' ? '#000000' : '#ffffff',
    backgroundColor: theme,
    animation: {
      duration: duration,
      timingFunc: 'linear'
    }
  });
  that.bg_color = wx.createAnimation({
    duration: duration,
    timingFunction: 'linear',
  });
  that.bg_color.opacity(0).step();
  that.setData({
    color: theme,
    bg: that.bg_color.export()
  });
  if (title != 'custom') wx.setNavigationBarTitle({
    title: title,
  })
  setTimeout(() => {
    that.setData({ showMask: false });
    if (cb) return cb();
  }, duration * .8)
};



/**
 * 跳转渐出动画
 * @param {object}   that   调用对象
 * @param {str}       url   跳转地址
 * @param {int}  duration   动画时长
 */
function onHide(that, url, theme, duration, cb) {
  duration = duration || DEFULT_DURATION,
    theme = theme || '#ffffff';
  that.setData({ showMask: true });
  wx.setNavigationBarColor({
    frontColor: '#ffffff',
    backgroundColor: '#ffffff',
    animation: {
      duration: duration,
      timingFunc: 'ease'
    }
  });
  that.bg_color = wx.createAnimation({
    duration: duration,
    timingFunction: 'ease',
  })
  that.bg_color.opacity(1).step();
  that.setData({ bg: that.bg_color.export() })
  setTimeout(() => {
    if (typeof url == 'string') wx.nav(url);
    if (typeof url == 'number') wx.navigateBack({ delta: url });
    if (cb) return cb();
  }, duration)
};


/**
 * 跳转渐出动画
 * @param {object}   that   调用对象
 * @param {str}     delta   返回级数
 * @param {int}  duration   动画时长
 */
// function onBack(that, delta, theme, duration, cb) {
//   duration = duration || DEFULT_DURATION,
//     theme = theme || '#ffffff';
//   that.setData({ showMask: true });
//   wx.setNavigationBarColor({
//     frontColor: '#ffffff',
//     backgroundColor: '#ffffff',
//     animation: {
//       duration: duration,
//       timingFunc: 'ease'
//     }
//   });
//   that.bg_color = wx.createAnimation({
//     duration: duration,
//     timingFunction: 'ease',
//   })
//   that.bg_color.opacity(1).step();
//   that.setData({ bg: that.bg_color.export() })
//   setTimeout(() => {
//     if (url) wx.navigateBack({
//       delta: delta,
//     })
//     if (cb) return cb();
//   }, duration)
// };


/**
 * 封装渐入动画
 * @param {object}    that    调用对象
 * @param {arr[str]}  param   分辨编号
 */
function show(that, param, cb) {
  var animation = wx.createAnimation({ duration: 0 });
  for (let i = 0; i < param.length; ++i) {
    animation.opacity(0).step()
    var json = '{"' + param[i] + '":""}'
    json = JSON.parse(json);
    json[param[i]] = animation.export()
    that.setData(json)
  }
  animation = wx.createAnimation({
    duration: INTERVAL,
    timingFunction: 'ease',
  });
  for (let i = 0; i < param.length; ++i) {
    setTimeout(function () {
      animation.opacity(1).step()
      var json = '{"' + param[i] + '":""}'
      json = JSON.parse(json);
      json[param[i]] = animation.export()
      that.setData(json)
    }.bind(this), i * INTERVAL / 2 + 200);
  }
  setTimeout(function () {
    if (cb) return cb();
  }.bind(this), (param.length + 1) * INTERVAL / 2);
};


/**
 * 封装渐出动画
 * @param {object}    that    调用对象
 * @param {arr[str]}  param   分辨编号
 */
function hide(that, param, cb) {
  var animation = wx.createAnimation({ duration: 0 });
  for (let i = 0; i < param.length; ++i) {
    animation.opacity(1).step()
    var json = '{"' + param[i] + '":""}'
    json = JSON.parse(json);
    json[param[i]] = animation.export()
    that.setData(json)
  }
  animation = wx.createAnimation({
    duration: INTERVAL,
    timingFunction: 'ease',
  });
  for (let i = 0; i < param.length; ++i) {
    setTimeout(function () {
      animation.opacity(0).step()
      var json = '{"' + param[i] + '":""}'
      json = JSON.parse(json);
      json[param[i]] = animation.export()
      that.setData(json)
    }.bind(this), i * INTERVAL / 2 + 200);
  }
  setTimeout(function () {
    if (cb) return cb();
  }.bind(this), (param.length + 1) * INTERVAL / 2 );
};


/**
 * 封装水平滑动渐入动画
 * @param   {object}  that    调用对象
 * @param {arr[str]}  param   分辨编号
 * @param    {float}  px      水平坐标
 * @param    {float}  opacity 新透明度
 * @return    {func}  cb      回调函数
 */
function slide_h(that, param, px, opacity, delay, cb) {
  var animation = wx.createAnimation({
    duration: INTERVAL,
    timingFunction: 'ease',
  });
  for (let i = 0; i < param.length; ++i) {
    setTimeout(function () {
      animation.translateX(px).opacity(opacity).step();
      var json = '{"' + param[i] + '":""}';
      json = JSON.parse(json);
      json[param[i]] = animation.export();
      that.setData(json);
    }.bind(this), i * INTERVAL / 8);
  }
  setTimeout(function () {
    if (cb) return cb();
  }.bind(this), param.length * INTERVAL / 8 + delay * INTERVAL);
};

/**
 * 设定水平坐标
 * @param   {object}  that    调用对象
 * @param {arr[str]}  param   分辨编号
 * @param    {float}  px      水平坐标
 * @param    {float}  opacity 新透明度
 * @return    {func}  cb      回调函数
 */
function _slide_h(that, param, px, opacity, cb) {
  var animation = wx.createAnimation({ duration: 0 });
  for (let i = 0; i < param.length; ++i) {
    animation.translateX(px).opacity(opacity).step();
    var json = '{"' + param[i] + '":""}';
    json = JSON.parse(json);
    json[param[i]] = animation.export();
    that.setData(json);
  }
  if (cb) return cb();
};

/**
 * 封装垂直滑动渐入动画
 * @param   {object}  that    调用对象
 * @param {arr[str]}  param   分辨编号
 * @param    {float}  px      垂直坐标
 * @param    {float}  opacity 新透明度
 * @return    {func}  cb      回调函数
 */
function slide_v(that, param, px, opacity, delay, cb) {
  var animation = wx.createAnimation({
    duration: INTERVAL ,
    timingFunction: 'ease',
  });
  for (let i = 0; i < param.length; ++i) {
    setTimeout(function () {
      animation.translateY(px).opacity(opacity).step();
      var json = '{"' + param[i] + '":""}';
      json = JSON.parse(json);
      json[param[i]] = animation.export();
      that.setData(json);
    }.bind(this), i * INTERVAL / 8);
  }
  setTimeout(function () {
    if (cb) return cb();
  }.bind(this), param.length * INTERVAL / 8 + delay * INTERVAL);
};

/**
 * 设定垂直坐标
 * @param   {object}  that    调用对象
 * @param {arr[str]}  param   分辨编号
 * @param    {float}  px      垂直坐标
 * @param    {float}  opacity 新透明度
 * @return    {func}  cb      回调函数
 */
function _slide_v(that, param, px, opacity, cb) {
  var animation = wx.createAnimation({ duration: 0 });
  for (let i = 0; i < param.length; ++i) {
    animation.translateY(px).opacity(opacity).step();
    var json = '{"' + param[i] + '":""}';
    json = JSON.parse(json);
    json[param[i]] = animation.export();
    that.setData(json);
  }
  if (cb) return cb();
};





//向右滑动渐入渐出
function sliderightshow(that, param, px, opacity) {
  var animation = wx.createAnimation({
    duration: 800,
    timingFunction: 'ease',
  });
  animation.translateX(px).opacity(opacity).step()
  var json = '{"' + param + '":""}'
  json = JSON.parse(json);
  json[param] = animation.export()
  that.setData(json)
};


function encapsulate() {
  wx.onShow = onShow;   // 开场渐入
  wx.onHide = onHide;   // 跳转渐出
  // wx.onBack = onBack;   // 返回渐出
  wx.show = show;       // 元素渐入
  wx.hide = hide;
  wx.slide_v = slide_v;
  wx.slide_h = slide_h;
  wx._slide_h = _slide_h
  wx._slide_v = _slide_v;
}

module.exports = {
  encapsulate: encapsulate
}
